package com.example.base.utils;

import java.io.FileInputStream;
import java.security.MessageDigest;

public class FileMD5Checksum {

    public static void main(String[] args) throws Exception {
        String md5=getMD5Checksum("D:\\video\\test\\[电影天堂www.dytt89.com]93国际列车大劫案：莫斯科行动-2023_HD国语中英双字.mp4");
        System.out.println(md5);
    }

    public static String getMD5Checksum(String filename) throws Exception {
        byte[] b = createChecksum(filename);
        String result = "";

        for (int i = 0; i < b.length; i++) {
            result += Integer.toString((b[i] & 0xff) + 0x100, 16).substring(1);
        }
        return result;
    }

    private static byte[] createChecksum(String filename) throws Exception {
        MessageDigest complete = MessageDigest.getInstance("MD5");
        FileInputStream fis = new FileInputStream(filename);

        byte[] buffer = new byte[1024];
        int numRead;

        do {
            numRead = fis.read(buffer);
            if (numRead > 0) {
                complete.update(buffer, 0, numRead);
            }
        } while (numRead != -1);

        fis.close();
        return complete.digest();
    }
}
